import time
import tkinter

from widget import common as WidgetCommon


class ScoreBoard(tkinter.Canvas):
    def __init__(self, master, data, current_score=-1):
        tkinter.Canvas.__init__(self, master, bg='#FFDDFF')
        self.data = data  # List of score.
        self.w = master.W
        self.box = []
        self.current_score = current_score

    def display(self):
        w = self.w

        # <Score Board> -----------------------------------------------------------------------------------------------
        self.place(relx=0.5, x=-3.8 * w, rely=0.5, y=-6 * w, width=7.6 * w, height=12 * w)
        self.master.update()
        # -------------------------------------------------------------------------------------------------------------

        # <Title>
        title = WidgetCommon.Label(self, bg=None, text='RANKING', w=0.1 * w)
        title.place(relx=0.5, x=-2.6 * w, y=0.2 * w, width=5 * w, height=1 * w)

        # <Labels> ----------------------------------------------------------------------------------------------------
        self.update()
        flag = True
        for n in range(8):
            self.update()
            time.sleep(0.1)
            lb_num = WidgetCommon.Label(self, bg='White', w=0.14 * w, text=str(n + 1), fg='#333333')
            if n < 3:
                lb_num['bg'] = ['#FFFF99', '#DDDDDD', '#FFCC99'][n]
            lb_num.place(x=0.6 * w, y=1.3 * w + 1.3 * n * w, width=1.2 * w, height=1.2 * w)
            lb = WidgetCommon.Button(self, bg='White', w=0.14 * w, active_background='#EEEEEE', fg='#333333')
            if flag and self.data[n] == self.current_score:
                flag = False
                lb.fg = 'Red'
            if n < len(self.data):
                lb.text = str(self.data[n])
            lb.place(x=2 * w, y=1.3 * w + 1.3 * n * w, width=5 * w, height=1.2 * w)
        # -------------------------------------------------------------------------------------------------------------

        # <Back Button> -----------------------------------------------------------------------------------------------

        back_button = WidgetCommon.Button(master=self, text='X', w=0.08 * w,
                                          fg='White', bg='Red', active_background='#CC0000', command=self.back_md)
        back_button.place(relx=1, x=-1.1 * w, y=0.3 * w, width=0.8 * w, height=0.8 * w)

    def back_md(self):
        self.destroy()
